package com.ddstation.common.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.ddstation.common.property.SystemProperties;
import com.ddstation.common.util.CheckUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddstation.common.entity.DdPictureEntity;
import com.ddstation.common.mapper.DdPictureMapper;
import com.ddstation.common.model.ImageModel;
import com.ddstation.common.util.AliCloudOssUtil;

@Service @Lazy
public class ImageServiceImpl implements ImageService {
	@Inject
	private DdPictureMapper ddPictureMapper;
	
	@Transactional(readOnly=true)
	public List<ImageModel> fetchImage(String[] id) {
		// TODO Auto-generated method stub
		List<ImageModel> retList = new ArrayList<ImageModel>();
		
		for (String sid : id) {
			if (sid.trim().length() == 0)
				continue;
			DdPictureEntity entity = ddPictureMapper.selectOne(sid);
			if(CheckUtil.isEmpty(entity)){
				continue;
			}
			ImageModel image = new ImageModel();
			image.setId(entity.getId());
			image.setObjKey(entity.getPath());
			image.setOriginImgUrl(entity.getPath());
			if (entity.getPath().indexOf("data:image/png;base64") == 0) {
				//内网base64地址
				image.setFileType(-1);
				image.setOriginImgUrl(entity.getPath());
			} else if (entity.getPath().endsWith(".pdf") || entity.getPath().endsWith(".PDF")) {
				image.setFileType(1);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, entity.getPath()));
			} else if (entity.getPath().endsWith(".doc") || entity.getPath().endsWith(".docx") || entity.getPath().endsWith(".DOC") || entity.getPath().endsWith(".DOCX")) {
				image.setFileType(2);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, entity.getPath()));
			} else if (entity.getPath().endsWith(".xls") || entity.getPath().endsWith(".xlsx") || entity.getPath().endsWith(".XLS") || entity.getPath().endsWith(".XLSX")) {
				image.setFileType(3);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, entity.getPath()));
			} else {
				image.setFileType(0);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, entity.getPath()));
				image.setThumbImgUrl(AliCloudOssUtil.getThumbImgUrl(null, entity.getPath(), "@400h"));
			}
			
			retList.add(image);
		}
		
		return retList;
	}
	
	@Transactional(readOnly=true)
	public List<ImageModel> fetchImageByKey(String[] keys) {
		// TODO Auto-generated method stub
		List<ImageModel> retList = new ArrayList<ImageModel>();
		
		for (String key : keys) {
			if (key.trim().length() == 0)
				continue;

			ImageModel image = new ImageModel();
			image.setObjKey(key);
			if (key.indexOf("data:image/png;base64") == 0) {
				//内网base64地址
				image.setFileType(-1);
				image.setOriginImgUrl(key);
			} else if (key.endsWith(".pdf") || key.endsWith(".PDF")) {
				image.setFileType(1);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, key));
			} else if (key.endsWith(".doc") || key.endsWith(".docx") || key.endsWith(".DOC") || key.endsWith(".DOCX")) {
				image.setFileType(2);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, key));
			} else if (key.endsWith(".xls") || key.endsWith(".xlsx") || key.endsWith(".XLS") || key.endsWith(".XLSX")) {
				image.setFileType(3);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, key));
			} else {
				image.setFileType(0);
				image.setOriginImgUrl(AliCloudOssUtil.getOriginImgUrl(null, key));
				image.setThumbImgUrl(AliCloudOssUtil.getThumbImgUrl(null, key, "@400h"));
			}

			retList.add(image);
		}
		
		return retList;
	}
	
	@Transactional(readOnly=true)
	public List<ImageModel> fetchImage(String attachment) {
		if(StringUtils.isNotBlank(attachment)) {
			String[] id = attachment.split(",");
			if(id.length > 0)
				return fetchImage(id);
		}
		return null;
	}

	@Transactional(rollbackFor=Exception.class)
	public int addImage(String url) {
		// TODO Auto-generated method stub
		DdPictureEntity entity = new DdPictureEntity();
		entity.setPath(url);
		ddPictureMapper.insert(entity);
		return entity.getId();
	}

}
